import 'package:flutter/material.dart';

// 头像组件
class AvatarWidget extends StatelessWidget {
  final String imageUrl; // 头像图片URL
  final bool isOnline; // 是否在线
  final double size; // 头像尺寸

  const AvatarWidget({
    Key? key,
    required this.imageUrl,
    this.isOnline = false,
    this.size = 40,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // 头像图片
        CircleAvatar(
          radius: size / 2,
          backgroundImage: NetworkImage(imageUrl),
          // 图片加载失败时显示默认头像
          onBackgroundImageError: (_, __) {},
          child: imageUrl.isEmpty ? Icon(Icons.person, size: size * 0.6) : null,
        ),
        // 在线状态指示器
        if (isOnline)
          Positioned(
            right: 0,
            bottom: 0,
            child: Container(
              width: size / 4,
              height: size / 4,
              decoration: BoxDecoration(
                color: Colors.green,
                shape: BoxShape.circle,
                border: Border.all(
                  color: Theme.of(context).scaffoldBackgroundColor,
                  width: 2,
                ),
              ),
            ),
          ),
      ],
    );
  }
}
